<?php
@set_time_limit(0);
@ini_set("max_execution_time","0");
@ignore_user_abort(true);
//CONFIG -------------------------------------------------------
$proxyfile = "proxy.txt";
$goodsfile = "good.txt";
$badsfile = "bad.txt";
$threads = 200;
//--------------------------------------------------------------
function check_proxy ($url, $proxy, $params) {
	$user_agent = "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_REFERER, $url);
	curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
	curl_setopt($ch, CURLOPT_PROXY, $proxy);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_TIMEOUT, 11);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
	$result = curl_exec($ch);
	$info = curl_getinfo($ch);
	$code = $info['http_code'];
	$total_time = $info['total_time'];
	curl_close($ch);
	if ($code <> 0 && intval($total_time) < 8 && $code < 400)
	return true;
	else
	return false;
}

function url_my() {
	if (strlen(dirname ($_SERVER["PHP_SELF"]))==1) {
		return "http://".$_SERVER["HTTP_HOST"]."/proxy_checker.php";
	} else {
		return "http://".$_SERVER["HTTP_HOST"].str_replace ("\\", "/", dirname ($_SERVER["PHP_SELF"]))."/proxy_checker.php";
	}
}

function run_threads($count) {
	GLOBAL $proxy_by_thread;
	for ($x=0;$x<$count;$x++) {
		$user_agent = "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, url_my()."?tr=".$x."&fr=".($proxy_by_thread*$x)."&to=".($proxy_by_thread*($x+1)));
		curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_TIMEOUT, 1);
		curl_setopt($ch, CURLOPT_POST, 0);
		$result = curl_exec($ch);
	}
}

/// Часть запускающая всё вначале
if ((@$_GET['tr']=="") && (@$_GET['fr']=="") && (@$_GET['to']=="")) {
	$proxy_by_thread= round(count(file($proxyfile))/$threads,0);
	run_threads($threads);
	die("Потоки запущены\n");
}


$pfile  = file($proxyfile);
$tr  = (int)$_GET['tr'];
$from  = (int)$_GET['fr'];
$to  = (int)$_GET['to'];
for ($x=$from;$x<$to;$x++) {
	$prox=str_replace(array("\n","\r","\n\r","\r\n"),"",$pfile[$x]);
	if (check_proxy("http://efoxxi.dyndns.org/vk/pr",$prox,"x=x")) {
		$f = fopen ($tr.$goodsfile, "a+");
		flock($f,2);
		fwrite ($f, $prox."\n");
		flock($f,3);
		fclose ($f);
	} else {
		$f = fopen ($tr.$badsfile, "a+");
		flock($f,2);
		fwrite ($f, $prox."\n");
		flock($f,3);
		fclose ($f);
	}
}
?>